//
//  BViewController.swift
//  test
//
//  Created by lyl on 2019/12/12.
//  Copyright © 2019年 lyl. All rights reserved.
//

import UIKit

class BViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    private var imageShowCollectionView : UICollectionView!
    private var imagesArray: [String] = []
    var zoomView: YSZoomView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .blue
        
        imagesArray = folderFiles()
        MyLog(imagesArray)
        addSunviews()
    }
    
    private func addSunviews() {
        //图片展示界面
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width:(kCardWidth-contentMargin)/2, height:(kCardWidth-contentMargin)/2)
        //列间距,行间距,偏移
        layout.minimumInteritemSpacing = contentMargin
        layout.minimumLineSpacing = contentMargin
        layout.sectionInset = UIEdgeInsets(top: contentMargin, left: pageMargin, bottom: contentMargin, right: pageMargin)
        
        imageShowCollectionView = UICollectionView(frame: CGRect(x: 0, y: kSafeAreaTopHeight, width: kScreenWidth, height: kSafeAreaHeight), collectionViewLayout: layout)
        
        imageShowCollectionView.backgroundColor = .blue
        imageShowCollectionView.delegate = self
        imageShowCollectionView.dataSource = self
        //注册一个cell
        imageShowCollectionView.register(YLImageShowCell.self, forCellWithReuseIdentifier:"imageShowCell")
        view.addSubview(imageShowCollectionView)
        
        imageShowCollectionView.reloadData()
    }
    
    //遍历文件夹
    func folderFiles() -> [String] {
        let folderPath = kDocPath + "/YLImageCache"
        let manager = FileManager.default
        if !manager.fileExists(atPath: folderPath){
            return [String]()
        }
        do {
            let files = try manager.contentsOfDirectory(atPath: folderPath)
            var arr = [String]()
            for file in files {
                let path = folderPath + "/\(file)"
                arr.append(path)
            }
            
            return arr
        } catch {}
        //print("\(fileSize)")
        return [String]()
    }
    
    //MARK: - 代理
    //MARK: 集合视图代理
    //每个区的item个数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imagesArray.count
    }
    //自定义cell
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageShowCell", for: indexPath) as! YLImageShowCell
        let file = imagesArray[indexPath.row]
        cell.imageView.image = UIImage(contentsOfFile: file)
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: false)
        let cell: YLImageShowCell = collectionView.cellForItem(at: indexPath) as! YLImageShowCell
        let imageView = cell.imageView
        
        if zoomView == nil {
            let frame = UIScreen.main.bounds
            zoomView = YSZoomView(frame: frame)
        }
        if zoomView?.superview == nil {
            // 将windowLevel改成UIWindowLevelStatusBar
            UIApplication.shared.keyWindow?.windowLevel = UIWindow.Level.statusBar
            UIApplication.shared.keyWindow?.addSubview(zoomView!)
        }
        // 将坐标转化成keywindow坐标系下的坐标
        let imageViewFrame = CGRect(x: 0, y: 100, width: kScreenWidth, height: 200)
        zoomView?.originFrame = imageViewFrame
        zoomView?.image = imageView?.image
        zoomView?.show()
    }
}
